﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DaInterFace;
using DaInterFace.PDA;
using DaInterFace.PDA.P02;

namespace P02
{
    /// <summary>
    /// 获得质检取样信息
    /// </summary>
    public class A02 : IPDAChild
    {
        public void Code(IPDA handler, CodeResult CR, PDACodeUser user, String parameter)
        {
            P02A02P P = (P02A02P)handler.SerialApi.Deserialize(typeof(P02A02P), parameter, CR.SerialType);

            P02A02R R = new P02A02R();
            
            try
            {
                DataTable table = new DataTable();
                table.TableName = "SAMPS";

                foreach (SetOfBook book in handler.Data.GetAccounts().Values)
                {
                    StringBuilder sql = new StringBuilder();
                    sql.AppendFormat("Select A.FID,A.FBillNo BillNo,A.FInteger FCount,A.FCheckBox IsOver,A.FTime BuildTime,A.FText1 OrderNo,B.FName SupplierName,C.FName ItemName,{0} BaseId From KEW_PurchSampling A \r\n", book.FNumber);
                    sql.Append("Left Join t_Supplier B On A.FBase=B.FItemID \r\n");
                    sql.Append("Left Join KEW_PurchItem C On A.FBase1=C.FID \r\n");
                    sql.Append("Left Join KEW_PurchOrderQty D On A.FID_SRC1=D.FID \r\n");
                    sql.Append("Where A.FCheckBox1=0");
                    if (!P.ShowOver)
                    {
                        sql.Append(" AND A.FCheckBox=0");
                    }

                    Recordset rs = handler.Data.ExeProcedure(book.FNumber, sql.ToString());
                    if (rs.Enable)
                    {
                        table.Merge(rs.Data.Tables[0]);

                        R.Table = table;
                        CR.Content = handler.SerialApi.Serializer(typeof(P02A02R), R, CR.SerialType);
                    }
                    else
                    {
                        CR.Result = EnumResult.Error;
                        CR.Message = "数据库错误";
                        break;
                    }
                }
                CR.Message = String.Format("操作员{0}获取质检取样信息", user.UserName);
            }
            catch (Exception ex)
            {
                CR.Result = EnumResult.Error;
                CR.Message = String.Format("发生错误:{0}", ex.Message);
            }
        }
    }


}
